package com.probiz.estoremf.catalog.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.model.ProductFilterGroup;
import com.probiz.estore.product.model.ProductSearchCriteria;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;

public class ProductCategoryFrontController extends	GenericStoreFrontController<ProductCategory> {

	private ProductManager productManager ;
	private ProductCategoryManager productCategoryManager ;
	private ProdMainManager prodMainManager ;

	protected void initController() throws Exception {
		mgr = productCategoryManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}

	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		// -----start:获得商品目录------
		String uri = request.getRequestURI();
		Integer categoryId = 1;
		if(!uri.contains("/prod/index.html")){
			categoryId = productCategoryManager.getCategoryIdByUri(uri);
		}
		ProductCategory productCategory = productCategoryManager.getById(categoryId);
		
		if(productCategory.getHasChildren() == 1){
			ModelAndView mv = new ModelAndView("product/productCategoryList");
			mv.addObject("categoryId", categoryId);
			return mv;
		} else {
			// 没有找到相应目录不能访问,直接进入找不到或已删除页面
			if (productCategory == null || !productCategory.getStatus().equals(Constants.FLAG_TRUE)) {
				try {
					response.sendError(404);
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}
			// -----end:获得商品目录------
			
			SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
			
			// ------start:改变页面显示条数 只有不是默认值的时候才需改变------
			if (searchCriteria.getPageSize() == defaultPageSize) {
				searchCriteria.changePaging(searchCriteria.getPageNo(), 10);
			}
			// ------end:改变页面显示条数 只有不是默认值的时候才需改变------
			
			// ------start:构造前台查询条件------
			ProductSearchCriteria productSearchCriteria = new ProductSearchCriteria();
			
			bindOnly(request, productSearchCriteria, "productSearchCriteria");
			if (null != productCategory.getProductType()) {
				productSearchCriteria = prodMainManager.setProductQuickSearchCriteria(productSearchCriteria, productCategory, uri);
			}
			productSearchCriteria.setStatus(Product.STATUS_ACTIVE);
			if (null == productSearchCriteria.getOrderby()) {
				productSearchCriteria.setOrderby((short) 0);
			}
			productSearchCriteria.setCategoryPaths(new String[] { productCategory.getCategoryPath() });
			// ------end:构造前台查询条件------
			List<Product> productList = productManager.searchByCriteriaForController(productSearchCriteria, searchCriteria);
			
			ModelAndView mv = new ModelAndView("product/defaultProductList");
			String loadMore = request.getParameter("loadMore");
			if(StringUtils.isNotEmpty(loadMore)){
				mv = new ModelAndView("product/include/loadMore");
			}
			mv.addObject("productList", productList);
			mv.addObject("category", productCategory);
			mv.addObject("productSearchCriteria", productSearchCriteria);
			
			// ------start:把页面list的显示风格设置到attribute中------
			Cookie[] cookies = request.getCookies();
			if (!ArrayUtils.isEmpty(cookies)) {
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("listShowStyle")) {
						mv.addObject("listShowStyle", cookie.getValue());
					}
					if (cookie.getName().equals("compareCartStyle")) {
						mv.addObject("compareCartStyle", cookie.getValue());
					}
				}
			}
			
			// ------start:设置快速搜索条件------
			if (null != productCategory.getProductType()) {
				List<ProductFilterGroup> productFilterGroupList = prodMainManager.generateProductFilters(productCategory, uri,
						request.getQueryString());
				// ------start:将filter分类------
				List<List<ProductFilterGroup>> productFilterGroupLists = new ArrayList<List<ProductFilterGroup>>();
				List<ProductFilterGroup> normalTypeList = new ArrayList<ProductFilterGroup>();
				List<ProductFilterGroup> optionValueTypeList = new ArrayList<ProductFilterGroup>();
				for (ProductFilterGroup group : productFilterGroupList) {
					if (group.getType() == ProductFilterGroup.TYPE_NORMAL) {
						normalTypeList.add(group);
					} else if (group.getType() == ProductFilterGroup.TYPE_SKUOPTIONVALUE) {
						optionValueTypeList.add(group);
					}
				}
				productFilterGroupLists.add(normalTypeList);
				productFilterGroupLists.add(optionValueTypeList);

				// ------end: 将filter分类------
				mv.addObject("productFilterGroupLists", productFilterGroupLists);
				
				//标识显示的是按目录检索产品
				mv.addObject("type", "category");
			}
			return mv;
		}
	}
}
